﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;

namespace WuDada.Core.Post.Persistence
{
    public class SignUpDao : AdoDao, ISignUpDao
    {
        public INHibernateDao NHibernateDao { get; set; }                

        /// <summary>
        /// 新增SignUp
        /// </summary>
        /// <param name="signUpVO">被新增的SignUp</param>
        /// <returns>新增後的SignUp</returns>
        public SignUpVO CreateSignUp(SignUpVO signUpVO)
        {
            NHibernateDao.Insert(signUpVO);

            return signUpVO;
        }

        /// <summary>
        /// 取得SignUp By SignUpId
        /// </summary>
        /// <param name="signUpId">SignUpId</param>
        /// <returns>SignUp</returns>
        public SignUpVO GetSignUpById(int signUpId)
        {
            return NHibernateDao.GetVOById<SignUpVO>(signUpId);
        }

        /// <summary>
        /// 取得SignUp By PostId
        /// </summary>
        /// <param name="postId">PostId</param>        
        /// <returns>SignUp清單</returns>
        public IList<SignUpVO> GetSignUpListByPostId(int postId)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<SignUpVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.AddOrder(Order.Desc("CreateTime"));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<SignUpVO>(dCriteria);
        }

        /// <summary>
        /// 刪除SignUp
        /// </summary>
        /// <param name="signUpVO">被刪除的SignUp</param>
        public void DeleteSignUp(SignUpVO signUpVO)
        {
            NHibernateDao.Delete(signUpVO);
        }

        /// <summary>
        /// 更新SignUp
        /// </summary>
        /// <param name="signUpVO">被更新的SignUp</param>
        /// <returns>更新後的SignUp</returns>
        public SignUpVO UpdateSignUp(SignUpVO signUpVO)
        {
            NHibernateDao.Update(signUpVO);

            return signUpVO;
        }

        /// <summary>
        /// 取得SignUp By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>SignUp清單</returns>
        public IList<SignUpVO> GetSignUpListByPostId(int postId, string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<SignUpVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));           

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<SignUpVO>(dCriteria);
        }

        /// <summary>
        /// 取得SignUp By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>SignUp清單</returns>
        public IList<SignUpVO> GetSignUpListByPostId(int postId, int pageIndex, int pageSize, string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<SignUpVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<SignUpVO>(dCriteria, pageIndex, pageSize);
        }

        /// <summary>
        /// 取得SignUp筆數 By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <returns>SignUp清單</returns>
        public int CountSignUpListByPostId(int postId)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<SignUpVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));            

            return NHibernateDao.CountByDetachedCriteria(dCriteria);
        }

        /// <summary>
        /// 確認是否已經有報名
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="email">Email</param>
        /// <returns>是否已經有報名</returns>
        public bool GetSignUpByPostIdAndEmail(int postId, string email)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<SignUpVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.Add(Expression.Eq("Email", email));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 動態查詢筆數SignUp
        /// </summary>
        /// <param name="where">搜尋語法，用Where...order by ...</param>
        /// <returns>筆數</returns>
        public int CountSignUpByWhere(string where)
        {
            return NHibernateDao.CountByWhere<SignUpVO>(where);
        }

        /// <summary>
        /// 動態查詢SignUp
        /// </summary>
        /// <param name="where">搜尋語法，用Where...order by ...</param>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <returns>搜尋結果</returns>
        public IList<SignUpVO> SearchSignUpByWhere(string where, int pageIndex, int pageSize)
        {
            return NHibernateDao.SearchByWhere<SignUpVO>(where, pageIndex, pageSize);
        }
    }
}
